ActionLink 显示错误 url
ActionLink shows wrong url
我在路由 table 中有一些预定义的 URLs 实际上指向相同的控制器和动作,但语言不同。例如
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", lang="en" } // Parameter defaults
);
routes.MapRoute(
"Contact2", // Route name
"iletisim", // URL with parameters
new { controller = "Home", action = "Contact", lang="tr" } // Parameter defaults
);
当我在视图中使用 Html.ActionLink
和 运行 应用程序时,URL 显示第一条路线,例如:
@Html.ActionLink(Resources.Contact, "Contact", "Home")
当当前语言为 tr
时呈现的页面 URL 为:/contact
而应为 /iletisim
。
当我更改路由映射的顺序时,en
语言页面显示错误 URL。
我该如何解决这个问题?
匹配来自 ActionLink
的路由时,必须考虑该路由生成的所有参数。该框架将始终 return 提供的路由值的第一个匹配项。
在你的例子中,你提供了路由值:
controller = "Home"
action = "Contact"
考虑到缺少其他信息,这将匹配您的 Contact
路线(第一条带有 controller = "Home", action = "Contact"
的路线)。
如果要匹配特定的路由,则还需要传递lang
路由值。
@Html.ActionLink(Resources.Contact, "Contact", "Home", new { lang = "tr" }, null)
这将匹配 Contact2
路线。
或者,您可以将 lang
放入页面的 URL 中,这将确保它是当前请求的一部分,如 [=34= 所示] 路由中的 MVC 5 文化和 url
。然后 MVC 会自动将此值提供给所有 ActionLinks
和其他基于 UrlHelper
的方法。
我在路由 table 中有一些预定义的 URLs 实际上指向相同的控制器和动作,但语言不同。例如
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", lang="en" } // Parameter defaults
);
routes.MapRoute(
"Contact2", // Route name
"iletisim", // URL with parameters
new { controller = "Home", action = "Contact", lang="tr" } // Parameter defaults
);
当我在视图中使用 Html.ActionLink
和 运行 应用程序时,URL 显示第一条路线,例如:
@Html.ActionLink(Resources.Contact, "Contact", "Home")
当当前语言为 tr
时呈现的页面 URL 为:/contact
而应为 /iletisim
。
当我更改路由映射的顺序时,en
语言页面显示错误 URL。
我该如何解决这个问题?
匹配来自 ActionLink
的路由时,必须考虑该路由生成的所有参数。该框架将始终 return 提供的路由值的第一个匹配项。
在你的例子中,你提供了路由值:
controller = "Home"
action = "Contact"
考虑到缺少其他信息,这将匹配您的 Contact
路线(第一条带有 controller = "Home", action = "Contact"
的路线)。
如果要匹配特定的路由,则还需要传递lang
路由值。
@Html.ActionLink(Resources.Contact, "Contact", "Home", new { lang = "tr" }, null)
这将匹配 Contact2
路线。
或者,您可以将 lang
放入页面的 URL 中,这将确保它是当前请求的一部分,如 [=34= 所示] 路由中的 MVC 5 文化和 url
。然后 MVC 会自动将此值提供给所有 ActionLinks
和其他基于 UrlHelper
的方法。